package com.gxu.sjjg;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Login extends JFrame{
    public Login(){
        initComponents();
        frame1.setVisible(true);//①要加这句使窗口可见，否则就只是一个默认窗口，啥也没有。②要放在上一句的后面，因为initComponents()会创建窗口对象，否则frame1和pannel1是null
        panel1.setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//设置前台关闭后程序结束
    }

    //登录界面
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        frame1 = new JFrame();
        panel1 = new JPanel();
        name = new JTextField();
        password = new JPasswordField();
        namelabel = new JLabel();
        passlabel = new JLabel();
        loginbutton = new JButton();
        loginbutton2 = new JButton();
        label1 = new JLabel();
        bgp = new JLabel();

        //======== frame1 ========
        {
            frame1.setResizable(false);
            frame1.setTitle("\u6210\u7ee9\u5206\u6790\u7cfb\u7edf");
            frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame1.setAutoRequestFocus(false);
            var frame1ContentPane = frame1.getContentPane();
            frame1ContentPane.setLayout(null);

            //======== panel1 ========
            {
                panel1.setLayout(null);

                //---- name ----
                name.setBackground(new Color(238, 238, 238));
                panel1.add(name);
                name.setBounds(155, 105, 165, 30);

                //---- password ----
                password.setBackground(new Color(238, 238, 238));
                panel1.add(password);
                password.setBounds(155, 160, 160, 30);

                //---- namelabel ----
                namelabel.setText("用户名:");
                namelabel.setLabelFor(password);
                namelabel.setForeground(Color.black);
                namelabel.setFont(namelabel.getFont().deriveFont(namelabel.getFont().getSize() + 1f));
                panel1.add(namelabel);
                namelabel.setBounds(105, 110, 60, 25);

                //---- passlabel ----
                passlabel.setText("\u5bc6   \u7801\uff1a");
                passlabel.setForeground(Color.black);
                passlabel.setFont(passlabel.getFont().deriveFont(passlabel.getFont().getSize() + 1f));
                panel1.add(passlabel);
                passlabel.setBounds(105, 165, 50, 25);

                //---- loginbutton ----
                loginbutton.setText("\u767b\u5f55");
                panel1.add(loginbutton);
                loginbutton.setBounds(new Rectangle(new Point(180, 225), loginbutton.getPreferredSize()));

                //---- loginbutton2 ----
                loginbutton2.setText("\u767b\u5f55");
                panel1.add(loginbutton2);
                loginbutton2.setBounds(0, 380, 78, 30);

                //---- label1 ----
                label1.setText("\u6210\u7ee9\u5206\u6790\u7cfb\u7edf");
                label1.setFont(new Font("\u534e\u6587\u6977\u4f53", Font.BOLD, 26));
                label1.setBackground(new Color(238, 238, 238));
                label1.setForeground(Color.black);
                panel1.add(label1);
                label1.setBounds(140, 20, 180, 40);

                //---- bgp ----
                bgp.setIcon(new ImageIcon(getClass().getResource("/myimage/loginimg.jpg")));
                bgp.setOpaque(true);
                panel1.add(bgp);
                bgp.setBounds(0, 0, 455, 330);
            }
            frame1ContentPane.add(panel1);
            panel1.setBounds(0, 0, 450, 330);

            frame1ContentPane.setPreferredSize(new Dimension(455, 360));
            frame1.pack();
            frame1.setLocationRelativeTo(frame1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        //为按钮添加事件监听
        loginbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {//点击登录按钮后判断输入的账号密码是否正确。
                String thename=name.getText(),thepass=new String(password.getPassword());
                if(name.getText().equals("")||thepass.equals(""))//判断输入是否为空
                    JOptionPane.showMessageDialog(null, "用户名或密码不能为空！", "提示！", JOptionPane.WARNING_MESSAGE);
                else {
                    File f = new File("src/file/manager.txt");
                    try {
                        BufferedReader bfr = new BufferedReader(new FileReader(f));
                        String s;
                        thename="用户名："+thename;//注意判断空后再加"用户名："！！
                        thepass="密码："+thepass;
                        boolean tag = false;
                        while ((s = bfr.readLine()) != null) {//逐行读取数据
                            if (s.equals(thename)) {//用户名正确
                                if (bfr.readLine().equals(thepass)) {//密码匹配
                                    tag = true;
                                    break;
                                }
                            }
                        }
                        if (tag)//如果输入的账号密码正确则跳转到功能菜单界面
                            new Fuctionframe();
                        else
                            JOptionPane.showMessageDialog(null, "用户名或密码错误！", "警告", JOptionPane.WARNING_MESSAGE);
                        bfr.close();
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                }
            }
        });
    }
    // 定义成员变量
    private JFrame frame1;
    private JPanel panel1;
    private JTextField name;
    private JPasswordField password;
    private JLabel namelabel;
    private JLabel passlabel;
    private JButton loginbutton;
    private JButton loginbutton2;
    private JLabel label1;
    private JLabel bgp;
}
